package com.info.controller;

import com.info.bll.UserBLL;
import com.info.dal.UserDAL;
import com.info.model.UserInfo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "modifyUserPassServlet", value = "/admin/modifyUserPassServlet")
public class modifyUserPassServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端表单传递过来数据
        String oldpass = request.getParameter("oldpass");
        String newpass = request.getParameter("newpass");

        System.out.println(oldpass+":"+newpass);
        //获取当前登录用户的id
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute("UserSession");
        //打印当前登录用户的信息
        System.out.println(user.getUser_id());

        UserInfo userInfo = new UserInfo();
        userInfo.setUser_id(user.getUser_id());
        userInfo.setUsername(user.getUsername());
        userInfo.setUserpass(oldpass);
        userInfo.setNewpass(newpass);

        UserBLL userBLL = new UserBLL();
        if(userBLL.modityUserPassByUserId(userInfo)){
            //修改成功
            System.out.println("修改密码成功");
            response.sendRedirect("success.jsp");
        }else{
            System.out.println("修改密码失败");
            response.sendRedirect("error.jsp");
        }
    }
}
